/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaEmpresas.java
 *
 * Created on 10/02/2011, 18:55:29
 */
package user_gui;

import elemento.Empresa;
import in_out.ComunicaBD;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractListModel;
import javax.swing.ListModel;

/**
 *
 * @author Jônatas
 */
public class ListaEmpresas extends javax.swing.JFrame {

    private ContainsEmpresa parentComponent;

    /** Creates new form ListaEmpresas */
    public ListaEmpresas(ContainsEmpresa parentComponent) {
        this.parentComponent = parentComponent;
        initComponents();
        fillEmpresasList();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listEmpresas = new javax.swing.JList();
        buttonOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Selecione Empresas");
        setResizable(false);

        listEmpresas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listEmpresas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listEmpresasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(listEmpresas);

        buttonOK.setText("OK");
        buttonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
                    .addComponent(buttonOK))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonOK)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-568)/2, (screenSize.height-561)/2, 568, 561);
    }// </editor-fold>//GEN-END:initComponents

    private void finish() {
        Object obj = listEmpresas.getSelectedValue();
        if (obj == null) {
            return;
        }

        String empresa = obj.toString();
        String idEmpresa = empresa.substring(0, empresa.indexOf(" ")).trim();

        parentComponent.setEmpresaText(idEmpresa);

        this.dispose();
    }

    private void buttonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOKActionPerformed
        finish();
    }//GEN-LAST:event_buttonOKActionPerformed

    private void listEmpresasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listEmpresasMouseClicked
        if (evt.getClickCount() == 2) {
            finish();
        }
    }//GEN-LAST:event_listEmpresasMouseClicked

    private void fillEmpresasList() {
        List<Empresa> empresas = null;
        try {
            empresas = FramePrincipal.COMUNICABD.selectAllEmpresa("empresa");
        } catch (SQLException ex) {
            Logger.getLogger(ListaEmpresas.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (empresas == null || empresas.isEmpty()) {
            return;
        }
        ListModel model = new MyListModel(empresas);
        listEmpresas.setModel(model);
    }

    public static class MyListModel extends AbstractListModel {

        private List<String> empresasStr;

        public MyListModel(List<Empresa> empresas) {
            this.empresasStr = builtStringList(empresas);
        }

        public int getSize() {
            return empresasStr.size();
        }

        public Object getElementAt(int index) {
            return empresasStr.get(index);
        }

        private List<String> builtStringList(List<Empresa> empresas) {

            Collections.sort(empresas);
            List<String> strList = new ArrayList<String>();

            for (Empresa bean : empresas) {
                String temp = "" + bean.getId();
                temp += " - " + bean.getNome();
                strList.add(temp);
            }

            return strList;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonOK;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listEmpresas;
    // End of variables declaration//GEN-END:variables
}
